<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
  
if(isset($_POST['submitBtn']))
{
  if(strlen($_POST['newItem']) > 0)
  {
    $insertItem = "INSERT INTO item (ItemName)
                   VALUES('".$_POST['newItem']."')";
    $insertItemRes = mysql_query($insertItem);
    $itemId = mysql_insert_id();
  }
  else
  {
    $itemId = $_POST['itemSelect'];
  }
  
  if(strlen($_POST['newSize']) > 0)
  {
    $insertSize = "INSERT INTO size (sizeName)
                   VALUES('".$_POST['newSize']."')";
    $insertSizeRes = mysql_query($insertSize);
    $sizeId = mysql_insert_id();
  }
  else
  {
    $sizeId = $_POST['sizeSelect'];
  }
  
  if(strlen($_POST['newCompany']) > 0)
  {
    $insertCompany = "INSERT INTO company (companyName)
                   VALUES('".$_POST['newCompany']."')";
    $insertCompanyRes = mysql_query($insertCompany);
    $companyId = mysql_insert_id();
  }
  else
  {
    $companyId = $_POST['companySelect'];
  }
  
  if(strlen($_POST['newQuality']) > 0)
  {
    $insertQuality = "INSERT INTO quality (qualityName)
                   VALUES('".$_POST['newQuality']."')";
    $insertQuality = mysql_query($insertQuality);
    $qualityId = mysql_insert_id();
  }
  else
  {
    $qualityId = $_POST['qualitySelect'];
  }
  
  if(strlen($_POST['newColor']) > 0)
  {
    $insertColor = "INSERT INTO color (colorName)
                   VALUES('".$_POST['newColor']."')";
    $insertColor = mysql_query($insertColor);
    $colorId = mysql_insert_id();
  }
  else
  {
    $colorId = $_POST['colorSelect'];
  }
  if(strlen($_POST['designName']) > 0)
  {
    $insertDesign = "INSERT INTO design (designName)
                   VALUES('".$_POST['designName']."')";
    $insertDesign = mysql_query($insertDesign);
    $designId = mysql_insert_id();
  }
  else
  {
    $designId = $_POST['designSelect'];
  }
  
  $dbpath = $_FILES['subItemImage']['name'];
  $Path   = "images/img/".$dbpath;
  move_uploaded_file($_FILES['subItemImage']['tmp_name'], $Path);
  $insertQuality = "INSERT INTO subitem (itemId, sizeId, companyId, qualityId, colorId, designId, itemImage)
                           values (".$itemId.",
                                   ".$sizeId.",
                                   '".$companyId."',
                                   '".$qualityId."',
                                   '".$colorId."',
                                   '".$designId."',
                                   '".$dbpath."') ";
  $qualityRes = mysql_query($insertQuality);
  
  if(!$qualityRes)
  {
  	echo "insert FAIL";
  }
  else
  {
    header("Location:itemCreate.php");
  }
}
  
  $items = array();
  $i = 0; 
  $selectItem = mysql_query("SELECT itemId,itemName 
                                    FROM item");
  while($itemRows=mysql_fetch_array($selectItem))
  {
    $items['itemId'][$i]  =$itemRows['itemId'];
    $items['itemName'][$i]=$itemRows['itemName'];
    $i++;
  }
  //Size Select
  $size = array();
  $j = 0;
  $selectSize = mysql_query("SELECT sizeId, sizeName
                                    FROM size");
  while($sizeRows=mysql_fetch_array($selectSize))
  {
    $size['sizeId'][$j] = $sizeRows['sizeId'];
    $size['sizeName'][$j] = $sizeRows['sizeName'];
    $j++;
  }
  //Select Company
  $companys = array();
  $k = 0;
  $selectcompany = mysql_query("SELECT companyId, companyName
                                    FROM company");
  while($companyRows=mysql_fetch_array($selectcompany))
  {
    $companys['companyId'][$k] = $companyRows['companyId'];
    $companys['companyName'][$k] = $companyRows['companyName'];
    $k++;
  }
  //Select Quality
  $quality = array();
  $x = 0;
  $selectquality = mysql_query("SELECT qualityId, qualityName
                                    FROM quality");
  while($qualityRows=mysql_fetch_array($selectquality))
  {
    $quality['qualityId'][$x] = $qualityRows['qualityId'];
    $quality['qualityName'][$x] = $qualityRows['qualityName'];
    $x++;
  }
  //Select Color
  $color = array();
  $m = 0;
  $selectColor = mysql_query("SELECT colorId, colorName
                                    FROM color");
  while($colorRows=mysql_fetch_array($selectColor))
  {
    $color['colorId'][$m]   = $colorRows['colorId'];
    $color['colorName'][$m] = $colorRows['colorName'];
    $m++;
  }
  $design = array();
  $m = 0;
  $selectDesign = mysql_query("SELECT designId, designName
                                    FROM design");
  while($designRows=mysql_fetch_array($selectDesign))
  {
    $design['designId'][$m] = $designRows['designId'];
    $design['designName'][$m] = $designRows['designName'];
    $m++;
  }
  include("./bottom.php");
  $smarty->assign('items', $items);
  $smarty->assign('size', $size);
  $smarty->assign('color', $color);
  $smarty->assign('design', $design);
  $smarty->assign('companys', $companys);
  $smarty->assign('quality', $quality);
  $smarty->display("itemCreate.tpl");  
?>